import openpyxl
from  conf.settings import DATA_PATH


def get_row_values(file_name,sheet_name,row_no):
    '''
    获取某行的数据---参数名称列表
    :param file_name:
    :param sheet_name:
    :param row_no:
    :return:
    '''

    file_path = DATA_PATH + f'/{file_name}'
    workbook = openpyxl.load_workbook(file_path)
    sheet = workbook[sheet_name]
    data = []
    columns = sheet.max_column
    for i in range(1,columns+1):
        data.append(sheet.cell(row=row_no,column=i).value)
    return data

def get_test_data(file_name,sheet_name):
    '''
    得到具体的参数值（第二行一下的）返回类似[[xxxx],[xxx]]
    :param file_name:
    :param sheet_name:
    :return:
    '''
    file_path = DATA_PATH + f'/{file_name}'
    workbook = openpyxl.load_workbook(file_path)
    sheet = workbook[sheet_name]

    row_data = []
    row_nums = sheet.max_row
    column_nums = sheet.max_column
    for i in range(2,row_nums+1):
        single_row_data = []
        for j in range(1,column_nums+1):
            single_row_data.append(sheet.cell(row=i,column=j).value)
        row_data.append(single_row_data)
    return row_data

def write_cell(file_name,sheet_name,row_no,col_no,content):
    '''
    写入数据（测试结果）
    :param file_name:
    :param sheet_name:
    :param row_no:
    :param col_no:
    :param content:
    :return:
    '''

    try:
        file_path = DATA_PATH + f'/{file_name}'
        workbook = openpyxl.load_workbook(file_path)
        sheet = workbook[sheet_name]

        sheet.cell(row=row_no,column=col_no).value = content
        workbook.save(file_path)
    except Exception as e:
        print(e)

if __name__ == '__main__':
    print(get_row_values('testData.xlsx','location',2))
    lis = get_test_data('testData.xlsx','location')
    for i in lis:
        print(lis)